<template>
  <div class="systemConfig">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane
        :label="item.key"
        :name="index+''"
        v-for="(item,index) in systemConfigCateList"
        :key="index"
      ></el-tab-pane>
    </el-tabs>
    <h3>
      <el-button
        type="primary"
        @click="clearSystemConfigCache"
        v-if="permissions_obj.id_65||permissions_obj.id_all"
      >清除系统配置缓存</el-button>
    </h3>
    <div class="box">
      <div class="boxItem" v-for="(item,index) in dataList" :key="index">
        <div class="row" v-if="item.tag_name=='radio'">
          <span>{{item.name}}：</span>
          <el-radio-group v-model="item.svalue">
            <el-radio label="1">开启</el-radio>
            <el-radio label="0">禁用</el-radio>
          </el-radio-group>
        </div>
        <div class="row2" v-if="item.tag_name=='input'">
          <div class="row">
            <span>{{item.name}}：</span>
            <el-input v-model="item.svalue" placeholder="请输入" clearable item.tag_attr></el-input>
          </div>
          <div class="ps" v-if="item.remark">{{item.remark}}</div>
        </div>
        <div class="row2" v-if="item.tag_name=='textarea'">
          <div class="row">
            <span>{{item.name}}：</span>
            <el-input v-model="item.svalue" placeholder="请输入内容" clearable type="textarea" :rows="3"></el-input>
          </div>
          <div class="ps" v-if="item.remark">{{item.remark}}</div>
        </div>
        <div class="row2" v-if="item.tag_name=='select'">
          <div class="row">
            <span>{{item.name}}：</span>
            <el-select v-model="item.svalue" placeholder="请选择" item.tag_attr>
              <el-option
                v-for="item in item.tag_vlist_arry"
                :key="item.label"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </div>
          <div class="ps" v-if="item.remark">{{item.remark}}</div>
        </div>
      </div>
      <div class="boxItem">
        <div class="row">
          <span></span>
          <el-button
            type="primary"
            @click="saveInfo"
            v-if="permissions_obj.id_64||permissions_obj.id_all"
          >保存</el-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "systemConfig",
  data() {
    return {
      activeName: 0,
      systemConfigCateList: [],
      initData: {},
      dataList: [],
      // 权限管理
      permissions: [],
      permissions_obj: {},
      sgroup: ""
    };
  },
  created() {
    //权限判断
    this.permissions = this.$store.state.permissions;
    let oneOff = false;
    this.permissions.forEach((item, index) => {
      if (item == -1) {
        oneOff = true;
      } else {
        this.permissions_obj["id_" + item] = item;
      }
    });
    if (oneOff) {
      this.permissions_obj.id_all = true;
    } else {
      this.permissions_obj.id_all = false;
    }
  },
  mounted() {
    this.getSystemConfigCate();
  },
  methods: {
    //获取系统配置详情
    async getSystemConfigInfo() {
      this.dataList=[];
      let parames = {
        handshake: "",
        data: {
          sgroup: this.sgroup
        }
      };
      let res = await this.$axios.post(
        this.$api.systemConfigInfo,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.initData = res.data;
        Object.keys(this.initData).forEach(key => {
          this.dataList.push(this.initData[key]);
        });
        console.log(this.dataList);
        //当是select的时候，转换tag_vlist的格式
        this.dataList.forEach((item, index) => {
          if (item.tag_name == "select") {
            let objList = [];
            Object.keys(JSON.parse(item.tag_vlist)).forEach((it, idx) => {
              let obj = {};
              obj.label = JSON.parse(item.tag_vlist)[it];
              obj.value = it;
              objList.push(obj);
            });
            item.tag_vlist_arry = objList;
            console.log(item.tag_vlist_arry);
          }
        });
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //保存配置信息
    async saveInfo() {
      let obj = {};
      this.dataList.forEach((item, index) => {
        obj[item.skey] = item.svalue;
      });
      let parames = {
        handshake: "",
        data:{
          sgroup:this.sgroup,
          input:obj
        } 
      };
      let res = await this.$axios.post(
        this.$api.systemConfigSave,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: "保存成功",
          type: "success"
        });
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //清除系统配置缓存
    async clearSystemConfigCache() {
      let parames = {
        handshake: "",
        data: {
          sgroup: this.sgroup
        }
      };
      let res = await this.$axios.post(
        this.$api.systemUpdateCache,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: "清除缓存成功",
          type: "success"
        });
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //获取系统配置分类
    async getSystemConfigCate() {
      let parames = {
        handshake: "",
        data: {}
      };
      let res = await this.$axios.post(
        this.$api.systemConfigType,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        let data = res.data;
        Object.keys(data).forEach((item, index) => {
          this.systemConfigCateList.push({
            key: data[item],
            value: item
          });
        });
        this.sgroup = this.systemConfigCateList[this.activeName].value;
        this.getSystemConfigInfo();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    handleClick(tab, event) {
      this.systemConfigCateList.forEach((item, index) => {
        if (item.key == tab.label) {
          this.sgroup = item.value;
        }
      });
      this.getSystemConfigInfo();
    }
  }
};
</script>
<style scoped>
h3 {
  padding: 0.2rem;
  color: #3c763d;
}
h3 span {
  margin-right: 0.4rem;
}
.systemConfig {
  width: 100%;
  padding-bottom: 0.3rem;
}
.box {
  width: 100%;
  display: flex;
  /* justify-content: center; */
  flex-direction: column;
  align-items: center;
}
.row {
  width: 100%;
  display: flex;
  align-items: center;
}
.ps {
  width: 80%;
  text-align: left;
  padding-left: 2rem;
  color: #ca674c;
}
.row span {
  display: flex;
  width: 2rem;
}
.row2 {
  width: 100%;
}
.row2 .row {
  width: 100%;
}
.boxItem {
  width: 80%;
  margin-top: 0.2rem;
}
.el-select {
  width: 100%;
}
>>> .el-tabs__item {
  height: 80px;
  line-height: 80px;
}
>>> .el-tabs__header {
  padding-left: 20px;
}
</style>